let app=new Vue({
    el:'#app',
    data:{
        admin:JSON.parse(sessionStorage.getItem('admin')),
        log:{}
    },
    created(){
        // 查询当前管理员的最后一条日志记录
       $.ajax({
        url:'http://localhost:8080/log/selectAll',
        type:'get',
        data:{userId:this.admin.userId},
        success:function(vo){
             if(vo.code==200){
                 app.log=vo.data[0];
             }
        }
    });
    },
    methods:{
        update:function(){
            $.ajax({
                url:'http://localhost:8080/user/update',
                type:'post',
                data:{realName:$("#realName").val(),phone:$("#phone").val(),userId:this.admin.userId},
                success:function(vo){
                    if(vo.code==200){
                        // 修改成功重新设置用户信息会话存储数据
                        let newAdmin={
                            userId:app.admin.userId,
                            userName:app.admin.userName,
                            password:app.admin.password,
                            realName:$("#realName").val(),
                            phone:$("#phone").val(),
                            sex:app.admin.sex,
                            age:app.admin.age,
                            birthday:app.admin.birthday,
                            userLevel:app.admin.userLevel,
                            role:app.admin.role,
                            image:app.admin.image,
                            status:app.admin.status,
                            createTime:app.admin.createTime
                        }
                        sessionStorage.setItem("admin",JSON.stringify(newAdmin));
                        app.admin=JSON.parse(sessionStorage.getItem("admin"));


                        layer.msg(vo.data,{offset:'t',icon:1});
                    }else{
                        layer.msg(vo.message,{offset:'t',icon:5});
                    }
                }
            });
        },
        updatePass:function(){
            let oldPass = $("#oldPass").val();
            let newPass = $("#newPass").val();
            let newPass2 = $("#newPass2").val();
            
            if(oldPass=='' || newPass=='' || newPass2==''){
                layer.msg("请填入完整信息",{offset:'t',icon:2});
                return;
            }
            if(newPass!=newPass2){
                layer.msg("两次密码输入不一致，请重试",{offset:'t',icon:2});
                return;
            }
            if(oldPass!=this.admin.password){
                layer.msg("旧密码输入错误",{offset:'t',icon:2});
                return;
            }

            $.ajax({
                url:'http://localhost:8080/user/update',
                type:'post',
                data:{password:newPass,userId:this.admin.userId},
                success:function(vo){
                    if(vo.code==200){

                        layer.msg(vo.data,{offset:'t',icon:1});
                        
                        // 修改成功重新设置用户信息会话存储数据
                        let newAdmin={
                            userId:app.admin.userId,
                            userName:app.admin.userName,
                            password:newPass,
                            realName:app.admin.realName,
                            phone:app.admin.phone,
                            sex:app.admin.sex,
                            age:app.admin.age,
                            birthday:app.admin.birthday,
                            userLevel:app.admin.userLevel,
                            role:app.admin.role,
                            image:app.admin.image,
                            status:app.admin.status,
                            createTime:app.admin.createTime
                        }
                        sessionStorage.setItem("admin",JSON.stringify(newAdmin));
                        app.admin=JSON.parse(sessionStorage.getItem("admin"));
                        
                    }else{
                        layer.msg(vo.message,{offset:'t',icon:2});
                    }
                }
            });

        },
        showTwo:function(){
            $("#one").hide();
            $("#two").css('display','inline-block');
        },
        showOne:function(){
            $("#one").show();
            $("#two").hide();
        }
    }
});